const db = require("../db/index.js");
const crypto = require("crypto");
const { basePath } = require("../config/index.js");
fs = require("fs");

exports.uploadImage = (req, res) => {
  const uuid = crypto.randomUUID();
  console.log(req.files);
  const from = req.body.from;
  const originName = req.files[0].originalname;
  const fileName = req.files[0].filename;
  const insertSql = "insert into images set ?";
  const fileurl = basePath + req.files[0].path;
  db.query(
    insertSql,
    {
      uuid: uuid,
      origin_name: originName,
      fileName: fileName,
      from: from,
      account: "Test33",
      url: fileurl,
    },
    (err, result) => {
      if (err) return res.cc(err);
      res.send({
        code: 0,
        message: "上传成功",
        data: {
          uuid: uuid,
          filename: fileName,
          from: from,
          url: fileurl,
        },
      });
    }
  );
};
